package com.itheima.homework.bh1604;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Set;

/*
【编号：1604】现需要将一些学生对象（属性包含姓名和年龄），与其地址，一一对应存储，并查看存储效果，要保证学生对象不重复，
请编写程序实现以上需求，运行效果如下：
Student{name='小红', age=20}---北京海淀
Student{name='小黄', age=22}---北京朝阳
Student{name='小白', age=25}---北京海淀
Student{name='小蓝', age=23}---北京顺义
Student{name='小绿', age=21}---北京昌平
 */
public class Test4 {
    public static void main(String[] args) {
        //创建LinkedHashMap集合，特点：存取有序
        LinkedHashMap<Student, String> hm = new LinkedHashMap<>();
        Student s1 = new Student("小红", 20);
        Student s2 = new Student("小黄", 22);
        Student s3 = new Student("小白", 25);
        Student s4 = new Student("小蓝", 23);
        Student s5 = new Student("小绿", 21);
        hm.put(s1, "北京海淀");
        hm.put(s2, "北京朝阳");
        hm.put(s3, "北京海淀");
        hm.put(s4, "北京顺义");
        hm.put(s5, "北京昌平");
        //遍历打印，通过KeySet()方法获取所有的Key，封装到Set集合中返回
        Set<Student> students = hm.keySet();
        //遍历Set集合获取每一个key
        for (Student student : students) {
            //通过get(key)获得对应的value值
            String value = hm.get(student);
            //遍历打印
            System.out.println(student + "---" + value);
        }
    }
}
class Student {
    private String name;
    private int age;
    public Student() {
    }
    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}